﻿using System;
using System.IO;
using System.Globalization;
using System.Windows.Controls;

namespace XamlTorrent.Common.Validation
{
    class FilePathValidationRule : ValidationRule
    {
        private bool validateEmptyPath = true;
        public bool ValidateEmptyPath
        {
            get { return validateEmptyPath; }
            set { validateEmptyPath = value; }
        }

        private string fileDneMessage = "File does not exist";

        /// <summary>
        /// Message to return when the file path does not exist.
        /// </summary>
        public string FileDneMessage
        {
            get { return fileDneMessage; }
            set { fileDneMessage = value; }
        }

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            // Determine string value of the input safely.
            // null |-> null, otherwise use its ToString() value.
            string strValue = (value == null) ? null : value.ToString();

            if (String.IsNullOrEmpty(strValue) && !validateEmptyPath)
                return new ValidationResult(true, null);
            else
                return new ValidationResult(File.Exists(strValue), fileDneMessage);
        }
    }
}
